package com.jeecms.cms.manager.assist.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.dao.assist.CmsOrderDao;
import com.jeecms.cms.entity.assist.CmsOrderExt;
import com.jeecms.cms.entity.assist.CmsOrder;
import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.cms.manager.assist.CmsOrderExtMng;
import com.jeecms.cms.manager.assist.CmsOrderMng;
import com.jeecms.cms.manager.main.CmsSiteMng;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;

@Service
@Transactional
public class CmsOrderMngImpl implements CmsOrderMng {
	@Transactional(readOnly = true)
	public Pagination getPage(Integer siteId, Integer userId, Boolean recommend,
			Boolean checked, boolean desc, boolean cacheable, int pageNo,
			int pageSize) {
		return dao.getPage(siteId, userId, recommend, checked, desc, cacheable,
				pageNo, pageSize);
	}

	@Transactional(readOnly = true)
	public List<CmsOrder> getList(Integer siteId, 
			Boolean recommend, Boolean checked, boolean desc,
			boolean cacheable, int first, int max) {
		return dao.getList(siteId,  recommend, checked, desc, cacheable,
				first, max);
	}

	@Transactional(readOnly = true)
	public CmsOrder findById(Integer id) {
		CmsOrder entity = dao.findById(id);
		return entity;
	}

	public CmsOrder save(CmsOrder bean, CmsOrderExt ext,
			 String ip) {
		bean.setIp(ip);
		bean.setCreateTime(new Timestamp(System.currentTimeMillis()));
		bean.init();
		dao.save(bean);
		cmsOrderExtMng.save(ext, bean);
		return bean;
	}

	public CmsOrder save(CmsUser member, Integer siteId, 
			String ip, String title, String content, String email,
			String phone, String qq,String companyName) {
		CmsOrder order = new CmsOrder();
		order.setMember(member);
		order.setSite(cmsSiteMng.findById(siteId));
		order.setIp(ip);
		CmsOrderExt ext = new CmsOrderExt();
		ext.setTitle(title);
		ext.setContent(content);
		ext.setEmail(email);
		ext.setPhone(phone);
		ext.setQq(qq);
		ext.setCompanyName(companyName);
		return save(order, ext, ip);
	}

	public CmsOrder update(CmsOrder bean, CmsOrderExt ext) {
		Updater<CmsOrder> updater = new Updater<CmsOrder>(bean);
		bean = dao.updateByUpdater(updater);
		cmsOrderExtMng.update(ext);
		return bean;
	}

	public CmsOrder deleteById(Integer id) {
		CmsOrder bean = dao.deleteById(id);
		return bean;
	}

	public CmsOrder[] deleteByIds(Integer[] ids) {
		CmsOrder[] beans = new CmsOrder[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private CmsOrderExtMng cmsOrderExtMng;
	private CmsSiteMng cmsSiteMng;
	private CmsOrderDao dao;

	@Autowired
	public void setDao(CmsOrderDao dao) {
		this.dao = dao;
	}

	@Autowired
	public void setCmsOrderExtMng(CmsOrderExtMng cmsOrderExtMng) {
		this.cmsOrderExtMng = cmsOrderExtMng;
	}

	@Autowired
	public void setCmsSiteMng(CmsSiteMng cmsSiteMng) {
		this.cmsSiteMng = cmsSiteMng;
	}
}